package com.eqdd.yiqidian.ui.mine.space.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.coorchice.library.SuperTextView
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.CompanyDiaryBean
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.kotlin.imgPathPreview
import com.eqdd.yiqidian.kotlin.listImagePreview
import com.eqdd.yiqidian.ui.yiqiyue.ArticleInfoActivity
import com.eqdd.yiqidian.utils.TimeUtil
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.fragment_company_space.view.*
import org.jetbrains.anko.support.v4.startActivity
import java.util.*

/**
 * Created by jhj on 18-7-4.
 */
class DiaryFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_company_space, container, false)
        initView(view)
        return view
    }

    fun initView(view: View) {
        val companyId = arguments?.getInt(Config.COMPANY_ID, -1)
        if (companyId == null || companyId == -1) {
            return
        }

        var pageNum = 0
        val mvcHelper = MVCCoolHelper<List<CompanyDiaryBean>>(view.coolRefreshView)
        val list = arrayListOf<CompanyDiaryBean>()
        view.recyclerView.layoutManager = activity?.let { LinearLayoutManager(it) }
        view.recyclerView.addItemDecoration(DividerLineItemDecoration(activity))
        val slimAdapterEx = SlimAdapterEx.create()
                .register<CompanyDiaryBean>(R.layout.list_item_149, SlimInjector<CompanyDiaryBean> { data, injector ->
                    injector?.imageCircle(R.id.iv_head, data?.iphoto)
                            ?.text(R.id.stv_name, data?.upname)
                            ?.text(R.id.tv_depart, TimeUtil.formatWebTime(data?.createTime))
                            ?.text(R.id.tv_content, data?.content)
                            ?.text(R.id.tv_title, data?.title)
                            ?.gone(R.id.iv_option)
                            ?.with<ImageView>(R.id.iv_image) {
                                if (data?.image != null)
                                    activity?.listImagePreview(data.image, it)
                            }
                            ?.clicked(R.id.iv_image) {
                                data?.image?.let { it1 -> activity?.imgPathPreview(it1) }
                            }
                            ?.with<SuperTextView>(R.id.stv_see) {
                                activity?.let { act ->
                                    if (data?.browseCount ?: 0 > 0) {
                                        it.text = data?.browseCount.toString()
                                    } else {
                                        it.text = "查看"
                                    }
                                }

                            }
                            ?.with<SuperTextView>(R.id.stv_zan) {
                                activity?.let { act ->
                                    if (data?.zanCount ?: 0 > 0) {
                                        it.text = data?.zanCount.toString()
                                    } else {
                                        it.text = "赞"
                                    }
                                }
                            }
                            ?.with<SuperTextView>(R.id.stv_comment) {
                                activity?.let { act ->
                                    if (data?.commentCount ?: 0 > 0) {
                                        it.text = data?.commentCount.toString()
                                    } else {
                                        it.text = "评论"
                                    }
                                }
                            }
                })
                .attachTo(view.recyclerView)
                .updateData(list as List<CompanyDiaryBean>?) as SlimAdapterEx<CompanyDiaryBean>
        ItemClickSupport.addTo(view.recyclerView).setOnItemClickListener { recyclerView, position, v ->
            startActivity<ArticleInfoActivity>(Config.ID to slimAdapterEx.getDataItem(position).id)

        }
        val dataSource = ModelRx2DataSource<CompanyDiaryBean>(ModelRx2DataSource.OnLoadSource<CompanyDiaryBean> { page, register ->
            if (page == 1) {
                pageNum = 0
            }
            OkGo.post<HttpResultRows<CompanyDiaryBean>>(HttpConfig.BASE_URL + HttpConfig.COMPANY_DIARY_LIST)
                    .params("companyId", companyId)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResultRows<CompanyDiaryBean>>() {
                        override fun test() {
                            super.test()
                        }
                    }).adapt(FlowableBody<HttpResultRows<CompanyDiaryBean>>())
                    .flatMap({ result ->
                        pageNum = result.items.page
                        if (page == 1) {
                            mvcHelper.isAutoLoadMore = true
                        }
                        val dataList = if (result.items?.rows == null)
                            ArrayList()
                        else
                            result.items.rows as MutableList<CompanyDiaryBean>?
                        Flowable.just<MutableList<CompanyDiaryBean>>(dataList)
                    })
        }, 10)


        mvcHelper.setDataSource(dataSource)
        mvcHelper.adapter = slimAdapterEx
        mvcHelper.refresh()


    }
}